package com.onlyxiahui.of.muc;

import java.util.Date;

import org.dom4j.Element;
import org.jivesoftware.openfire.interceptor.PacketInterceptor;
import org.jivesoftware.openfire.interceptor.PacketRejectedException;
import org.jivesoftware.openfire.session.Session;
import org.xmpp.packet.JID;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;

import com.onlyxiahui.of.muc.dao.RoomChatDAO;
import com.onlyxiahui.of.muc.dao.UserChatDAO;
import com.onlyxiahui.of.muc.dao.UserDAO;

/**
 * @author XiaHui
 * @date 2017年11月21日 下午5:08:52
 */
public class MUCPacketInterceptor implements PacketInterceptor {

	public MUCPacketInterceptor() {
	}

	@Override
	public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed)
			throws PacketRejectedException {

		// incoming来判断是服务器发送的包还是接受的包（注：true为服务器接收的包；false为发出的包）
		// processed处理之前processed=false,处理之后就为processed=true;
		// if (incoming && processed) {
		// System.out.println("incoming:" + incoming);
		// System.out.println("processed:" + processed);
		// System.out.println(packet);
		// System.out.println(packet.getClass());
		// }
		// System.out.println("" + incoming + processed + "");
		if (incoming && !processed) {
			// System.out.println("接受：" + packet.getClass());
			// System.out.println(packet);

			if (packet instanceof Presence) {
				// JID to = packet.getTo();
				JID from = packet.getFrom();
				Presence presence = (Presence) packet;
				Presence.Type type = presence.getType();
				if (Presence.Type.unavailable == type) {
					Element e = presence.getElement();
					int c = e.nodeCount();
					// Element pe = e.element("priority");
					if (c == 0) {
						String jid = from.toBareJID();
						UserDAO.writeToDatabase(jid, "", new Date());
						// System.out.println(c);
						// System.out.println(pe);
						// System.out.println("离线");
						// System.out.println("to:" + to);
						// System.out.println("from:" + jid);
						// System.out.println(packet);
					}
				}
			}

			if (packet instanceof Message) {
				Message message = (Message) packet;
				Message.Type type = message.getType();

				if (Message.Type.chat == type) {
					Date date = new Date();
					String fromFullJid = message.getFrom().toBareJID();
					String toFullJid = message.getTo().toBareJID();
					String body = message.getBody();
					String xml = message.toXML();
					UserChatDAO.saveUserChatLog(fromFullJid, toFullJid, body, date, xml);
					// System.out.println("接受：" + message);
				} else if (Message.Type.groupchat == type) {
					Date date = new Date();
					String fromFullJid = message.getFrom().toBareJID();
					String toFullJid = message.getTo().toBareJID();
					String body = message.getBody();
					String xml = message.toXML();
					RoomChatDAO.saveRoomChatLog(fromFullJid, toFullJid, body, date, xml);
				}
			}
		}

		if (!incoming && processed) {
			// System.out.println("发送：" + packet.getClass());
			// System.out.println(packet);
		}
	}
}
